<style type="text/css">
table.center {
    margin-left: auto;
    margin-right: auto;
</style>


<body>

<div class="form">
	<table class = "center">
		<tr><td>
			<form action="index.php" method="post">
		    	<input type="submit" name="reset" value="Reset" />
			</form>
		</td></tr>
		<tr><td>
			<form action="index.php" method="post">
		    	<input type="submit" name="startPayment" value="StartPayment" />
			</form>
		</td></tr>
		<tr><td>
			<form action="index.php" method="post">
		    	<input type="submit" name="inquery" value="Inquery" />
			</form>
		</td></tr>
	</table>
</div>
</body>


<?php
require __DIR__ . '/moovpay.php';
    if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['StartPayment'])) {
        startPayment(1);
    }else if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['inquery'])) {
    	// your own way to get order id and order time 
		$json = file_get_contents("data.json");
		$jsonData = json_decode($json, true);
		$orderID = $jsonData["orderID"];
		$time = $jsonData['time'];
		echo "<br>";
    	inquiry($orderID, $time);
    }

    // function to start a payment with given amount, integer is expected. $1 = 100 here. 
    function startPayment($amount) {
    	$moovpay = new MoovPaySDK;
		$time = date('YmdHis');
		// set up rest as your spaces
		$securityCode = '600f7de7c2ea6e98fb3cefef';
		$merchantCode = '99987020033';
		$mid = '555070282990011';
		// use your own way to generate order ID.
		$orderID = uniqid();
		// set as your front end call back url
		$frontendURL = "https://xxx.com/frontend";
		// set as your back end call back url 
		$backendURL = 'https://1bahtbuy.com/pay/moovpay_url/frontend';// "https://xxx.com/backend";
		$moovpay->purchase($securityCode, $frontendURL, $backendURL, $merchantCode, $mid, $orderID, $time, '', $amount, '');

		// your own code to save any information you need
		$json = array("orderID" => $orderID, "time" => $time);
		var_dump($json);
		$fh = fopen("data.json", 'w');
		fwrite($fh, json_encode($json));
		fclose($fh);
    }

    function inquiry($orderID, $time) {
    	$moovpay = new MoovPaySDK;
		// set up rest as your spaces
		$securityCode = '600f7de7c2ea6e98fb3cefef';
		$merchantCode = '99987020033';
		$mid = '555070282990011';
		$result = $moovpay->inquiry($securityCode, $merchantCode, $mid, $orderID, $time);
		var_dump($result);
    }

    // pass the parameters from call back to verfy payment, do this in your backend call back url
    function onCallBack() {
    	$publicKey = '-----BEGIN PUBLIC KEY-----
h+WK0gmygeCf8XFxCFEaWKvgVgVXGU7N9nf1to7QY4gzeRmEYKBOwYj+jRemIkyY
fNz99YxzJJMd2LHqfK9+G8UCIl1dEU+aKr/9Ju+AEBlu2ZyRKlDHCm1SlBVOz5bS
medpUmwnF3CnK1U+I7UY93w6oLVLEe8rWSF0adtaf+UnPZa7jyDlUPqpj7N4RLPK
VwgvF2H7e/RDSpyVIpk1vkwGu6cyUY24e0lTi1esMOetTxBzhuoSD89biBqNawmc
IAbePZ8UGmMdVekPGPkItiLeRCmau8VxAKkJ7Zm/b8wnTtaPtJY0wIDAQAB
-----END PUBLIC KEY-----';
		$sdk = new MoovPaySDK;
		$verified = $sdk->verify($_POST, $publicKey);
		if ($verified) {
			// your code to handle payment success 
		}else {
			// your code to handle payment fail
		}
    }
?>